<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-01-14
 * Time: 13:50
 */
namespace smartybuilder\libs\bind;
class Table
{
    protected $action = '';
    protected $page = [
        'type'=>false,
        'limit'=>10000,
        'limits'=>[],
    ];
    protected $headtool = [
    ];
    protected $type = 'ordinary';
    protected $search = [];
    protected $columns = [];
    protected $linebutton = [];
    protected $total = [];
    /**设置配置
     * @param array $array
     * @return $this
     */
    public function setConfig($array=[]){
        if(!empty($array)){
            if(is_array($array)){
                foreach ($array as $key=>$value){
                    $this->config[$key] = $value;
                }
            }
        }
        return $this;
    }
    /**获取配置
     * @param null $str
     * @return array|bool|mixed
     */
    public function getConfig($str=null){
        if(!empty($str)){
            if(isset($this->config[$str])){
                return $this->config[$str];
            }else{
                return false;
            }
        }
        return $this->config;
    }

    /**设置获取数据的api接口
     * @param string $action
     * @return $this
     */
    public function setAction($action=''){
        $this->action = $action;
        return $this;
    }

    /**设置表格类型
     * @param string $type
     * @return $this
     */
    public function setType($type='ordinary'){
        $this->type = $type;
        return $this;
    }

    /**设置头部工具栏
     * @param string工具栏类型a和button $type
     * @param string文字 $desc
     * @param string请求api接口 $action
     * @param string $style
     * @return $this
     */
    public function setHeadTool($type='button',$desc='',$action='',$style=''){
        $this->headtool[] = [
          'type'=>$type,
          'desc'=>$desc,
          'action'=>$action,
          'style'=>$style,
        ];
        return $this;
    }

    /**设置搜索字段
     * @param string搜索的类型 $type
     * @param string搜索的描述 $desc
     * @param string搜索的name $name
     * @param array不同搜索 $option
     * @return $this
     */
    public function setSearch($type='input',$desc='',$name='',$option =['开启','关闭'],$hierarchy=1,$select_value=['id'=>'id','show'=>'name']){
        $this->search[] = [
          'type'=>$type,
          'desc'=>$desc,
          'name'=>$name,
          'option'=>$option,
          'hierarchy'=>$hierarchy,
          'select_value'=>$select_value,
        ];
        return $this;
    }

    /**设置表格右侧的按钮
     * @param string按钮类型 $type
     * @param string按钮文字 $desc
     * @param string按钮的api接口 $action
     * @param string样式 $style
     * @return $this
     */
    public function setLineButton($type='button',$desc='',$action='',$style=''){
        $this->linebutton[] = [
            'type'=>$type,
            'desc'=>$desc,
            'action'=>$action,
            'style'=>$style,
        ];
        return $this;
    }

    /**设置列
     * @param string $type
     * @param string $desc
     * @param string $name
     * @param string $style
     * @return $this
     */
    public function setColumn($type='input',$desc='',$name='',$action='',$style=''){
        $this->columns[] = [
          'type'=>$type,
          'desc'=>$desc,
          'name'=>$name,
          'action'=>$action,
          'style'=>$style,
        ];
        return $this;
    }

    /**设置是否开启分页
     * @param bool确认是否开启 $type
     * @return $this
     */
    public function setPage($type=true,$limit=10,$limits=[10,20,50,100]){
        $this->page = [
            'type'=>$type,
            'limit'=>$limit,
            'limits'=>$limits,
            ];
        return $this;
    }

    /**开启合计列的name的数组
     * @param array $total
     * @return $this
     */
    public function setTotal($total=[]){
        $this->total = $total;
        return $this;
    }
    public function fetch(){
        $str = include_once __DIR__.'/../config.php';
        $smarty = new \Smarty();
        $smarty ->template_dir = __DIR__.DIRECTORY_SEPARATOR."../Templates";
        $smarty ->compile_dir  = __DIR__.DIRECTORY_SEPARATOR."../Templates_c";
        $smarty ->left_delimiter   = "{{";
        $smarty ->right_delimiter  = "}}";
        $smarty->assign('path',$str['path']);
        $smarty->assign('headtool',$this->headtool);
        $smarty->assign('search',$this->search);
        $smarty->assign('action',$this->action);
        $smarty->assign('page',$this->page);
        $smarty->assign('columns',$this->columns);
        $smarty->assign('total',$this->total);
        $smarty->assign('linebutton',$this->linebutton);
        if($this->type=='ordinary'){
            return $smarty->display('table.html');
        }else{
            return $smarty->display('treetable.html');
        }
    }

}